<?php
namespace app\common\model;
use think\facade\Request;
use think\Model;
use think\facade\Config;

class Common extends Model{
    /*
     * 公共类变量
     * */
    protected  $table;
    protected  $sysconfig;

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->sysconfig = Config::get('sysconfig');
    }
    /*
     * 添加数据
     * */
    public function addRow($data = array()){
        return $this->allowField(true)->save($data);
    }

    /*
     * 统计数量
     * */
    public function countRows($where){
        return $this->where($where) ->count();
    }

    /*
     * 查询多条数据
     * */
    public function getAll($where,$field=''){
        return $this->where($where)->field($field)->select();
    }

    /*
     * 根据条件查询一条数据
     * */
    public function getOne($where,$field=''){
        return $this->where($where)->field($field)->find();
    }

    /*
     * 排序查询数据
     * */
    public function getAllOrder($where,$field,$order){
        return $this->where($where)->field($field)->order($order)->select();
    }

    //分页查询
    public function getPagelist($where,$field,$order){ 
        $limit = Request::param('limit')??10;
        $page = Request::param('page')??1;
        return $this->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->page($page)
            ->select();
    }

    /*
     * sum求和
     * */
    public function mathSum($where,$field){
        return $this->where($where)->sum($field);
    }

    //获取单独一个字段(字符串)
    public function fieldStr($where,$field){
        return $this->where($where)->value($field);
    }
    public function fieldMax($where,$field){
        return $this->where($where)->max($field);
    }

    // 自增1
    public function setOneinc($where,$field,$num=1){
        return $this->where($where)->setInc($field,$num);
    }
    //自减一
    public function setOnedec($where,$field,$num=1){
        return $this->where($where)->setDec($field,$num);
    }

}